import Vue from 'vue'
import VueRouter from 'vue-router'
// 引入首页vue
import home from './home'
import classify from './classify'
import Login from './login'
import user from './user'
import Cart from './cart'
import store from '../store'
Vue.use(VueRouter)

const routes = [
    ...home,
    ...classify,
    ...user,
    ...Login,
    ...Cart
]

const router = new VueRouter({
  routes
})
//全局前置路由守卫
router.beforeEach((to,from,next)=>{
  let isLogin =store.state.user.userIfo
    if(to.path.indexOf('/cart')!=-1||to.path.indexOf('/address')!=-1){//判断是否访问购物车、收货地址,是否需要拦截
        if(isLogin)//判断是否登录
          next()
        else
          next({
            path:'/login',
            query:{
              target:to.path
            }
          })
    }else
    next()

})
export default router
